<?php
/**
 * Created by Daniel X -- PhpStorm.  2020/7/24 15:30
 */

namespace App\Services;

use App\Http\Controllers\Traits\TransFormer;
use App\Models\Event;
use App\Models\InjuryTwo;
use App\Models\Patient;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\Types\Self_;

class PatientService
{
    use TransFormer;
    protected $model;
    protected $two;
    protected $injuryTwo;
    protected $event;

    public function __construct(Patient $model,InjuryService $two,InjuryTwo $injuryTwo,Event $event)
    {
        $this->model = $model;
        $this->two = $two;
        $this->injuryTwo = $injuryTwo;
        $this->event = $event;
    }

    public function create($data)
    {
        if (isset($data['patient_id'])) {
            $patient_id = $data['patient_id'];
            unset($data['patient_id']);
            $res = $this->model->where(['patient_id' => $patient_id])->first();
            if($res){
                $this->model->where(['patient_id' => $patient_id])->update($data);
            }else{
                $this->transFormerE(1001014, "更新失败");
            }
        } else {
            $data['open_id'] = Cookie::get('open_id');
            // $data['open_id'] = "oIOCB0V9Jz2i1vNMxiRTCK_t162c";
            $data['create_time'] = date("Y-m-d H:i:s");
            $patient_id = $this->model->insertGetId($data);
            if (!$patient_id) {
                $this->transFormerE(1001002, "添加失败");
            }
            // 同步创建此患者的二次检伤信息数据
            $this->two->createTwoInjuryTable($patient_id);
        }
        return $patient_id;
    }

    public function getPatientInfo($data)
    {
        $patient_id = $data['patient_id'];
        $res = $this->model->where(['patient_id'=>$patient_id])->get();
        if($res->isEmpty()) {
            $this->transFormerE(1001013, '获取患者详情失败');
        }
        $res = $res->toArray();
        foreach ($res[0] as $k => $v){
            if(empty($v) || $v == 'null'){
                $res[0][$k] = "";
            }
        }
        $dd['patient_id'] = $res[0]['patient_id'];
        $dd['name'] = $res[0]['name'];
        $dd['card'] = $res[0]['card'];
        $dd['gender'] = $res[0]['gender'] == 1?'男':($res[0]['gender'] == 2?'女':'');
        $dd['age'] = $res[0]['age'];
        $dd['province'] = $res[0]['province'];
        $dd['city'] = $res[0]['city'];
        $dd['area'] = $res[0]['area'];
        $dd['address'] = $res[0]['address'];
        $dd['initial_diagnosis'] = $res[0]['initial_diagnosis'];

        if(!empty($dd['initial_diagnosis'])){
            $dd['initial_diagnosis'] = explode(',',$dd['initial_diagnosis']);
        }
        return $dd;
    }

    public function getPatientList($where)
    {
        $open_id = Cookie::get('open_id');
        // $open_id = "oIOCB0V9Jz2i1vNMxiRTCK_t162c";  // 10984251

        if($where['sort'] == 2){
            $sort = "asc";
        }else{
            $sort = "desc";
        }

        $res = $this->model
            ->leftJoin($this->injuryTwo->table,$this->model->table.'.patient_id',$this->injuryTwo->table.'.patient_id')
            ->leftJoin($this->event->table,$this->model->table.'.event_id',$this->event->table.'.event_id')
            ->where(['open_id'=>$open_id])
            ->where(function ($query) use ($where){
                if(!empty($where['patient_name'])){
                    $query->where('name','like','%'.$where['patient_name'].'%');
                }
            })
            ->where(function ($query) use ($where){
                if(!empty($where['event_id'])){
                    $query->where($this->model->table.'.event_id','=',$where['event_id']);
                }
            })
            ->where(function ($query) use ($where){
                if(!empty($where['level_id'])){
                    $query->where('level','=',$where['level_id']);
                }
            })
            ->select($this->model->table.'.patient_id',$this->model->table.'.name','age','gender','event_name',$this->injuryTwo->table.'.create_time','level',$this->model->table.'.event_id')
            ->orderBy($this->model->table.'.patient_id',$sort)
            ->offset($this->execute($where['page'] ?? 1, $where['limit'] ?? 20))
            ->limit($where['limit'] ?? 20)
            ->get();
        if($res->isEmpty()){
            return [];
        }

        $res = $res->toArray();

        foreach ($res as $kk => $vv){
            $res[$kk]['gender'] = $vv['gender']==1?"男":($vv['gender']==2?"女":"");
            $res[$kk]['level_name'] = $vv['level']==1?"轻伤":($vv['level']==2?"中度":($vv['level']==3?"危重":($vv['level']==4?"已死亡":"")));
        }
        foreach ($res as $kk => $vv){
            foreach ($vv as $key => $val){
                if(empty($val) || $val == null){
                    $res[$kk][$key] = "";
                }
            }
        }
        return $res;
    }

}